package com.me.brainlessbirds.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.me.brainlessbirds.Assets;
import com.me.brainlessbirds.BrainlessGame;

public class MenuScreen extends BrainlessBirdsScreen
{

	private Stage				scene;
	public static final int		WIDTH		= 800;
	public static final int		LEVEL_WIDTH	= 1600;
	public static final int		HEIGHT		= 480;

	private TextureRegion		rgnBg		= new TextureRegion();
	private TextureRegion		rgnLogo		= new TextureRegion();

	private Camera				camera;
	private Matrix4				startMatrix	= null;
	private BitmapFont			font;
	private SpriteBatch			fontBatch;

	private static MenuScreen	current;

	// temps ecoule
	float						elapsedTime	= 0.0f;

	MenusButton					startButton;
	MenusButton					creditButton;

	public static MenuScreen get()
	{
		return current;
	}

	public MenuScreen()
	{
		current = this;
		scene = new Stage(LEVEL_WIDTH, HEIGHT, true);

		this.camera = new OrthographicCamera(WIDTH, HEIGHT);
		this.startMatrix = this.camera.combined.cpy();
		this.startMatrix.translate(new Vector3(-WIDTH / 2, -HEIGHT / 2, 0));
		this.camera.translate(new Vector3(WIDTH / 2, HEIGHT / 2, 0));

		scene.setCamera(this.camera);

		// assigner la scene comme gestionnaire de touch
		Gdx.input.setInputProcessor(scene);

		font = new BitmapFont();
		fontBatch = new SpriteBatch();

		Assets.get().load();

		this.rgnBg = new TextureRegion(Assets.get().textureTitleBackground);
		this.rgnLogo = new TextureRegion(Assets.get().textureLogo);

		Texture thx = Assets.get().texturePlayButton;
		startButton = new MenusButton(thx, WIDTH / 2, HEIGHT / 2, 256f, 128f);
		this.scene.addActor(startButton);
		Texture thx2 = Assets.get().textureCreditButton;
		creditButton = new MenusButton(thx2, WIDTH / 2, HEIGHT / 2 - 128, 256f, 128f);
		this.scene.addActor(creditButton);
	}

	@Override
	public void render(float delta)
	{
		Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

		// incrementer le temps ecoule
		elapsedTime += Gdx.graphics.getDeltaTime();

		// update / faire bouger les elements de la scene / animer les actions
		scene.act();
		// rendu des elements ajoutes a la scene
		this.camera.update();

		// decors
		scene.getSpriteBatch().setProjectionMatrix(this.startMatrix);
		scene.getSpriteBatch().begin();
		scene.getSpriteBatch().draw(this.rgnBg, 0, 0, GameScreen.WIDTH, GameScreen.HEIGHT);
		scene.getSpriteBatch().end();
		scene.getSpriteBatch().begin();
		scene.getSpriteBatch().draw(this.rgnLogo, 0, GameScreen.HEIGHT - (GameScreen.HEIGHT / 5) * 2, GameScreen.WIDTH, GameScreen.HEIGHT / 5);
		scene.getSpriteBatch().end();

		scene.draw();

		if (startButton.selected == true)
		{
			BrainlessGame.get().setScreen(new GameScreen());
		}

		if (creditButton.selected == true)
		{
			BrainlessGame.get().setScreen(new CreditScreen());
		}
	}

	@Override
	public void resize(int width, int height)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void show()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void hide()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void pause()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void resume()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose()
	{
		Assets.get().dispose();
		font.dispose();
		fontBatch.dispose();
	}

}
